package cn.rayYee.utils 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.ColorTransform;
	import pet_facebook.pet_tools.pet_panelloader.LoaderSwf;
	/**
	 * ...
	 * @author 
	 */
	public class LoadPetText extends Sprite
	{
		//loader swf
		private var _textContentLoader:LoaderSwf;
		private var _mContent:*;
		
		//text&string container
		private var _aTextContent:Array;
		private var _aTextSprite:Array;

		private var _iH_Pitch:Number;//字符间距
		private var _iR_Pitch:Number;//行距
		private var _iH_Column:int;//每行显示的字符数
		private var _nScale:Number;//缩放大小
		private var _uiColor:uint;//文本颜色
		/**
		 * 文本转图片[支持英文字母，数字，常用字符]
		 * @param	_sContents  文本内容text content
		 * @param	_iColor		文本颜色text color
		 * @param	_iColumn	每行显示的字符数maxChars a row
		 * @param	_iHPitch	字符间距text pitch
		 * @param	_iRPitch	行距
		 * @param	_scale		缩放大小text scale
		 * @param	_swfurl		text container url
		 */
		public function LoadPetText(_sContents:String, _iColor:uint, _iColumn:int, _iHPitch:Number, _iRPitch:Number,_scale:Number=1,_swfurl:String="game_release/release_text/ltext.swf") {
			_aTextSprite = [];
			_aTextContent = [];

			//加载图片库
			_textContentLoader = new LoaderSwf();
			_textContentLoader.loadingSwf(_swfurl, callBack);
			function callBack():void{
				_mContent = _textContentLoader.getContent as DisplayObject;
				var newStr:String = _sContents.toLowerCase();//讲所有大写字符转成小写
				
				_iH_Pitch = _iHPitch;
				_iR_Pitch = _iRPitch;
				_iH_Column = _iColumn;
				_nScale = _scale;
				_uiColor = _iColor;
			
				var i:int;
				var count:int = newStr.length;
				for (i = 0; i < count; i += 1 )	_aTextContent.push(newStr.substr(i, 1));
				for (i = 0; i < count; i += 1 ) loadContent(_aTextContent[i].toString());
				appearContents();
			}
		}
		private function appearContents():void{
			var _textSprite:Sprite = new Sprite();
			addChild(_textSprite);
			var i:int;
			var row:int;
			var count:int = _aTextSprite.length;
			for (i = 0; i < count; i += 1 )
			{
				if (i % _iH_Column == 0 && i != 0) {
					row ++;
					_aTextSprite[i].x = 0;
				}else if (i != 0) {
					//_aTextSprite[i].x = i * _iH_Pitch - row * _iH_Pitch * _iH_Column;
					_aTextSprite[i].x = _aTextSprite[i - 1].x + _aTextSprite[i - 1].width + _iH_Pitch;
				}
				if (i == 0) {
					_aTextSprite[i].x = 0;
				}
				_aTextSprite[i].scaleX = _aTextSprite[i].scaleY = _nScale;
				_aTextSprite[i].y = _iR_Pitch * row;
				_textSprite.addChild(_aTextSprite[i]);
			}
			var _colorTransform:ColorTransform = _textSprite.transform.colorTransform;
			_colorTransform.color = _uiColor;
			_textSprite.transform.colorTransform = _colorTransform;
		}
		private function loadContent(_sContent:String):void{
			var _sClassName:String = /^[,.?!:;']+$/.test(_sContent) ? "symbol_" + _sContent + "_mc" : /^[A-Z]+$/.test(_sContent) ? _sContent + "_mc" : /^[a-z]+$/.test(_sContent) ? _sContent + "_mc" : /^[0-9]+$/.test(_sContent) ? "number_" + _sContent + "_mc" : _sContent == " " ? "empty_mc" : "empty_mc";
			var contentClass:Class = _textContentLoader.getClass(_sClassName) as Class;
			var mysprite:Sprite = new contentClass() as Sprite;
			_aTextSprite.push(mysprite);
		}
	}

}